﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace DNA.Mvc.jQuery
{
    public class GridColumnBuilder : ViewComponentBuilder<GridColumn, GridColumnBuilder>
    {
        public GridColumnBuilder(GridColumn component, ViewContext context, IViewDataContainer container) : base(component, context, container) { }

        public GridColumnBuilder Title(string text)
        {
            Component.Title = text;
            return this;
        }

        public GridColumnBuilder Template(Action<object> tmpl)
        {
            Component.CellTemplate.Content = tmpl;
            return this;
        }

        public GridColumnBuilder Template(Func<object, object> tmpl)
        {
            Component.CellTemplate.InlineContent = tmpl;
            return this;
        }

        public GridColumnBuilder ClientTemplate(Action tmpl)
        {
            Component.ClientCellTemplate.Content = tmpl;
            return this;
        }

        public GridColumnBuilder ClientTemplate(Func<object, object> tmpl)
        {
            Component.ClientCellTemplate.InlineContent = tmpl;
            return this;
        }

        public GridColumnBuilder TextAlign(HorizontalAlign align)
        {
            Component.TextAlign = align;
            return this;
        }


    }

    public class GridColumnBuilder<TModel, TValue> : ViewComponentBuilder<GridColumn<TModel, TValue>, GridColumnBuilder<TModel, TValue>>
    {
        public GridColumnBuilder(GridColumn<TModel, TValue> component, ViewContext context, IViewDataContainer container) : base(component, context, container) { }

        public GridColumnBuilder<TModel, TValue> TextAlign(HorizontalAlign align)
        {
            Component.TextAlign = align;
            return this;
        }

        public GridColumnBuilder<TModel, TValue> Title(string text)
        {
            Component.Title = text;
            return this;
        }

        public GridColumnBuilder<TModel, TValue> Template(Action<TModel> tmpl)
        {
            this.Component.CellTemplate.Content = new Action<object>(dat => { tmpl.Invoke((TModel)dat); });
            return this;
        }

        public GridColumnBuilder<TModel, TValue> Template(Func<TModel, object> tmpl)
        {
            this.Component.CellTemplate.InlineContent =new Func<object,object>(dat=>{ return tmpl((TModel)dat);});
            return this;
        }

        public GridColumnBuilder<TModel, TValue> ClientTemplate(Action tmpl)
        {
            Component.ClientCellTemplate.Content = tmpl;
            return this;
        }

        public GridColumnBuilder<TModel, TValue> ClientTemplate(Func<object, object> tmpl)
        {
            Component.ClientCellTemplate.InlineContent = tmpl;
            return this;
        }

        public GridColumnBuilder<TModel, TValue> Format(string format)
        {
            Component.FormatString = format;
            return this;
        }
    }
}
